import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { STColumn, STComponent, STChange, STPage } from '@delon/abc/table';
import { SFSchema, SFComponent, SFRadioWidgetSchema } from '@delon/form';
import { HttpService, SupDic, GridComponent } from 'lbf';
import { ActivatedRoute } from '@angular/router';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ReuseTabService } from '@delon/abc/reuse-tab';
import { LbStepsComponent } from '@shared/components/lb-steps/lb-steps.component';
import { LbWjfwComponent } from '@shared/components/lb_wjfw/lbwjfw.component';
import { LbRepComponent } from '@shared/components/lb-rep/lb-rep.component';

@Component({
  selector: 'app-ybzh-lxbx',
  templateUrl: './lxbx.component.html',
})
export class YbzhLxbxComponent implements OnInit {
  dataset = [];
  PROJID = '1';
  DAD069 = '';
  date2: any = {};
  isVisible = false;
  isOkLoading = false;
  isPzVisible = false;
  modalPzTitle = '疾病编码';
  params = { sname: 'BJXX_QUERY', queryparas: {} };
  slSchema: SFSchema = {
    properties: {
      DAD095: {
        type: 'string',
        title: '',
        enum: [
          { label: '受理', value: '1' },
          { label: '不受理', value: '0' },
        ],
        // tslint:disable-next-line: no-object-literal-type-assertion
        ui: {
          widget: 'radio',
          // asyncData: () => of().pipe(delay(100)),
          change: console.log,
          grid: {
            span: 24,
          },
        } as SFRadioWidgetSchema,
        default: '1',
      },
      DAD098: {
        type: 'string',
        title: '受理建议',
        ui: {
          visibleIf: { DAD095: ['0'] },
          spanLabelFixed: 80,
          grid: { span: 24 },
        },
      },
    },
    ui: { grid: { span: 24 } },
  };

  constructor(
    private lbservice: HttpService,
    public msgSrv: NzMessageService,
    private supDic: SupDic,
    private reuseTabService: ReuseTabService,
    private activateroute: ActivatedRoute,
    private modalService: NzModalService,
  ) {
    this.reuseTabService.title = '医疗费用零星报销';
  }

  sdSchema: SFSchema = {
    properties: {
      AAB301: {
        type: 'string',
        title: '统筹区编码',
        enum: this.supDic.getSFDic('AAB301'),
        readOnly: true,
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      AAC002: {
        type: 'string',
        title: '证件号码',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AAC003: {
        type: 'string',
        title: '姓名',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AAE030: {
        type: 'string',
        title: '开始日期',
        readOnly: true,
        ui: {
          widget: 'date',
          grid: { span: 6 },
        },
      },
      AAE031: {
        type: 'string',
        title: '结束日期',
        readOnly: true,
        ui: {
          widget: 'date',
          grid: { span: 6 },
        },
      },
      AKB020: {
        type: 'string',
        title: '医院编码',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AKA078: {
        type: 'string',
        title: '就诊方式',
        readOnly: true,
        enum: this.supDic.getSFDic('AKA078'),
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      AKC264: {
        type: 'integer',
        title: '发票费用总额',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      BKC013: {
        type: 'number',
        title: '发票张数',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AAE009: {
        type: 'string',
        title: '收款开户名',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      AAE010: {
        type: 'string',
        title: '收款银行账号',
        readOnly: true,
        ui: { grid: { span: 6 } },
      },
      BAC048: {
        type: 'string',
        title: '收款开户行名称',
        readOnly: true,
        ui: { hidden: false, grid: { span: 6 } },
      },
      LS_DS1: {
        type: 'string',
        title: '电子发票列表',
        ui: { hidden: true, grid: { span: 6 } },
      },
    },
    ui: {
      spanLabelFixed: 130,
      grid: { span: 7 },
    },
  };

  bdSchema: SFSchema = {
    properties: {
      DAZ040: {
        type: 'string',
        title: '报销事由',
        enum: this.supDic.getSFDic('DAZ040'),
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      AKE045: {
        type: 'string',
        title: '病种类型',
        enum: this.supDic.getSFDic('AKE045'),
        ui: {
          widget: 'select',
          allowClear: true,
          grid: { span: 6 },
        },
      },
      AKC038: {
        type: 'string',
        title: '门诊住院号',
        ui: { grid: { span: 6 } },
      },
      AKE042: {
        type: 'string',
        title: '疾病编码',
        ui: {
          widget: 'custom',
          grid: { span: 6 },
        },
      },
      AKE046: {
        type: 'string',
        title: '疾病名称',
        readOnly: true,
        ui: {
          grid: { span: 6 },
        },
      },
      AAE013: {
        type: 'string',
        title: '备注',
        ui: { grid: { span: 6 } },
      },
      CKC009: {
        type: 'string',
        title: '交易流水号',
        ui: { hidden: true, grid: { span: 6 } },
      },
    },
    required: ['DAZ040', 'AKE045', 'AKE042', 'AKC038'],
    ui: {
      spanLabelFixed: 130,
      grid: { span: 3 },
    },
  };

  searchSchema: SFSchema = {
    properties: {
      AKE042: {
        type: 'string',
        title: '疾病编码',
      },
      AKE046: {
        type: 'string',
        title: '疾病名称',
      },
    },
  };

  jbColumns: STColumn[] = [
    { title: '疾病编码', index: 'AKE042' },
    { title: '疾病名称', index: 'AKE046' },
    {
      title: '操作区',
      buttons: [
        {
          text: '添加',
          click: record => this.tjjb(record),
        },
      ],
    },
  ];

  tabMode = 'bjxx';
  @ViewChild('sf', { static: false }) sf: SFComponent;
  @ViewChild('sf1', { static: false }) sf1: SFComponent;
  @ViewChild('sf2', { static: false }) sf2: SFComponent;
  @ViewChild('slxx', { static: false }) slxx: SFComponent;
  @ViewChild('wjfw', { static: false }) wjfw: LbWjfwComponent;
  @ViewChild('step', { static: true }) step: LbStepsComponent;
  @ViewChild('pzst', { static: false }) pzst: GridComponent;

  clsmcolumns: STColumn[] = [
    { title: '序号', index: 'RN', width: '10%' },
    { title: '材料名称', index: 'STUFFNAME', width: '50%' },
    { title: '材料数量', index: 'STUFFNUM', width: '25%' },
    { title: '收件类型', index: 'STUFFTYPE', width: '15%' },
  ];
  page: STPage = {
    show: false,
  };

  ngOnInit() {
    if (this.activateroute.snapshot.queryParams.PROJID) {
      this.PROJID = this.activateroute.snapshot.queryParams.PROJID;
      this.DAD069 = this.activateroute.snapshot.queryParams.DAD069;
      this.YB_QUERY();
      this.YBCL_QUERY();
    }
  }

  //打开情况下再次传入值
  _onReuseInit(): void {
    if (this.activateroute.snapshot.queryParams.PROJID) {
      this.PROJID = this.activateroute.snapshot.queryParams.PROJID;
      this.DAD069 = this.activateroute.snapshot.queryParams.DAD069;
      this.YB_QUERY();
      this.YBCL_QUERY();
    }
  }

  //事项进度
  selectTab(str: string) {
    this.tabMode = str;
    if (str === 'sxjd') {
    }
  }
  //界面赋值
  YB_QUERY() {
    this.lbservice
      .lbservice('YB_QUERY', { para: { PROJID: this.PROJID } })
      .then(resdata => {
        if (resdata.code < 1) {
          this.msgSrv.error(resdata.errmsg);
        } else {
          this.sf.formData = JSON.parse(resdata.message);
          this.sf.reset();
        }
      });
  }
  //材料列表
  YBCL_QUERY() {
    this.lbservice
      .lbservice('YBCL_QUERY', {
        para: { PROJID: this.PROJID },
      })
      .then(resdata => {
        if (resdata.code === 0) {
          this.msgSrv.error(resdata.errmsg);
        } else {
          this.dataset = resdata.message.list;
        }
      });
  }
  //受理保存
  lc_insert() {
    this.lbservice
      .lbservice('bxsl_insert', {
        para: {
          PROJID: this.PROJID,
          DAD069: this.DAD069,
          sf: this.sf.value,
          sf1: this.sf1.value,
          slxx: this.slxx.value,
        },
      })
      .then(resdata => {
        if (resdata.code < 1) {
          this.msgSrv.error(resdata.errmsg);
        } else {
          if (this.slxx.getValue('/DAD095') === '0') {
            this.msgSrv.info('操作成功');
            this.sf.formData = null;
            this.sf.reset();
            this.sf1.formData = null;
            this.sf1.reset();
          } else {
            this.msgSrv.info('保存成功');
            this.sf1.setValue('/CKC009', resdata.message.CKC009);
            this.isVisible = true;
            this.sf.formData = null;
            this.sf.reset();
            this.sf1.formData = null;
            this.sf1.reset();
          }
        }
      });
  }
  //疾病编码查询
  yp_query() {
    this.lbservice
      .lbservice('JB_QUERY', {
        para: { sf2: this.sf2.value },
      })
      .then(resdata => {
        if (resdata.code < 1) {
          this.msgSrv.error('未查询到相关疾病');
        } else {
          this.date2 = resdata.message.list;
        }
      });
  }
  handlePzCancel() {
    this.isPzVisible = false;
  }
  JbAdd() {
    this.sf1.setValue('/AKE042', null);
    this.sf2.refreshSchema();
    this.pzst.reload({});
    this.isPzVisible = true;
  }
  tjjb(rec) {
    this.sf1.setValue('/AKE042', rec.AKE042);
    this.sf1.setValue('/AKE046', rec.AKE046);
    this.isPzVisible = false;
  }
  handleOk(): void {
    this.isOkLoading = true;
    this.print();
    this.sf.formData = null;
    this.sf.reset();
    this.sf1.formData = null;
    this.sf1.reset();
    setTimeout(() => {
      this.isVisible = false;
      this.isOkLoading = false;
    }, 3000);
  }
  handleCancel(): void {
    this.isVisible = false;
    this.sf.formData = null;
    this.sf.reset();
    this.sf1.formData = null;
    this.sf1.reset();
  }
  print() {
    const modal = this.modalService.create({
      nzTitle: '打印',
      nzContent: LbRepComponent,
      nzComponentParams: {
        repid: '43',
        inpara: { DAD060: this.DAD069},
      },
      nzWidth: 880,
    });
  }
  // tslint:disable-next-line:all
  pname = '省端传入数据';
  // tslint:disable-next-line: member-ordering
  active = true;
  // tslint:disable-next-line: member-ordering
  pname1 = '本地获取数据';
  // tslint:disable-next-line: member-ordering
  active1 = true;
  // tslint:disable-next-line: member-ordering
  icon1 = 'caret-right';
  // tslint:disable-next-line: member-ordering
}
